export function getNodeStyle(id, that) {
	// 比如元素id: formContainer
	return new Promise((resolve, reject) => {
		uni.createSelectorQuery().in(that).select(`#${id}`).boundingClientRect(data => {
			console.log(data, 'node'); // 打印获取结果
			resolve(data)
		}).exec()
	})
}
export function draw_line(ctx, x1, y1, x2, y2, lw, fill) {


	return new Promise((resolve, reject) => {

		try {

			ctx.beginPath()
			ctx.setStrokeStyle(fill)
			ctx.setLineCap('round')
			ctx.setLineWidth(lw)
			ctx.moveTo(x1, y1)
			ctx.lineTo(x2, y2)
			ctx.stroke()
			resolve()
		} catch (e) {
			//TODO handle the exception
			reject(e)
		}


	})

}


export function drawText(ctx, str, leftWidth, initHeight, titleHeight, canvasWidth) {

	return new Promise((resolve, reject) => {

		try {
			let lineWidth = 0;
			let lastSubStrIndex = 0; //每次开始截取的字符串的索引
			for (let i = 0; i < str.length; i++) {
				lineWidth += ctx.measureText(str[i]).width;
				if (lineWidth > canvasWidth) {
					ctx.setFillStyle('#333')
					ctx.font = 'normal 12px PingFangSC-Regular, PingFang SC'
					ctx.fillText(str.substring(lastSubStrIndex, i), leftWidth, initHeight); //绘制截取部分
					initHeight += 15; //22为 文字大小20 + 2
					lineWidth = 0;
					lastSubStrIndex = i;
					titleHeight += 30;
				}
				if (i == str.length - 1) { //绘制剩余部分
					ctx.setFillStyle('#333')
					ctx.font = 'normal 12px PingFangSC-Regular, PingFang SC'
					ctx.fillText(str.substring(lastSubStrIndex, i + 1), leftWidth, initHeight);
				}
			}
			// 标题border-bottom 线距顶部距离
			titleHeight = titleHeight + 30;
			resolve(titleHeight);
		} catch (e) {
			//TODO handle the exception
			reject(0)
		}


	})




}
export function draw_text(ctx, str, x, y, fill, font) {
	return new Promise((resolve, reject) => {

		try {
			ctx.setFillStyle(fill)
			ctx.font = font
			ctx.fillText(str, x, y)
			resolve()
		} catch (e) {
			//TODO handle the exception
			reject(e)
		}


	})


}
// export function draw_rech(ctx, x, y, w, h, r, fill, hasBorder) {
// 	ctx.beginPath();
// 	ctx.moveTo(x + r, y);
// 	ctx.lineTo(x + w - r, y);
// 	ctx.arcTo(x + w, y, x + w, y + r, r);
// 	ctx.lineTo(x + w, y + h - r);
// 	ctx.arcTo(x + w, y + h, x + w - r, y + h, r);
// 	ctx.lineTo(x + r, y + h);
// 	ctx.arcTo(x, y + h, x, y + h - r, r);
// 	ctx.lineTo(x, y + r);
// 	ctx.arcTo(x, y, x + r, y, r);
// 	if (fill) {
// 		ctx.setFillStyle(fill)
// 		ctx.fill()
// 	}
// 	ctx.closePath();
// 	hasBorder && ctx.stroke();
// }
/**
 * 绘制圆角矩形
 * @param {Object} ctx - canvas组件的绘图上下文
 * @param {Number} x - 矩形的x坐标
 * @param {Number} y - 矩形的y坐标
 * @param {Number} w - 矩形的宽度
 * @param {Number} h - 矩形的高度
 * @param {Number} r - 矩形的圆角半径
 * @param {String} [c = 'transparent'] - 矩形的填充色
 */
export const draw_rech = (ctx, x, y, w, h, r, c = 'transparent') => {

	return new Promise((resolve, reject) => {

		try {
			if (w < 2 * r) {
				r = w / 2;
			}
			if (h < 2 * r) {
				r = h / 2;
			}

			ctx.beginPath();
			ctx.fillStyle = c;

			ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);
			ctx.moveTo(x + r, y);
			ctx.lineTo(x + w - r, y);
			ctx.lineTo(x + w, y + r);

			ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);
			ctx.lineTo(x + w, y + h - r);
			ctx.lineTo(x + w - r, y + h);

			ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);
			ctx.lineTo(x + r, y + h);
			ctx.lineTo(x, y + h - r);

			ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);
			ctx.lineTo(x, y + r);
			ctx.lineTo(x + r, y);

			ctx.fill();
			ctx.closePath();
			resolve()
		} catch (e) {
			//TODO handle the exception
			reject(e)
		}


	})

};
export function downImgs(urls) {
	let all = urls.map(link => {
		return new Promise((resolve, reject) => {
			uni.downloadFile({
				url: link,
				success(res) {
					resolve(res.tempFilePath)
				}
			})
		})
	})
	return new Promise((resolve, reject) => {
		Promise.all(all).then((res) => {
			console.log(res, 'allres');
			resolve(res)
		})
	})

}
export function downImg(url) {

	return new Promise((resolve, reject) => {
		uni.downloadFile({
			url: url,
			success: function(res) {
				console.log(res);
				resolve(res.tempFilePath)
			},
			fail(err) {

				console.log(err, 'ffff', url);
			}
		})
	})
}

export function draw_img(ctx, url, x, y, w, h, r) {
	return new Promise((resolve, reject) => {

		try {
			ctx.save()
			if (r) {
				draw_rech(ctx, x, y, w, h, r, '#fff', false)
				ctx.clip()
				ctx.drawImage(url, x, y, w, h)
			} else {
				ctx.drawImage(url, x, y, w, h)
			}

			ctx.restore()
			resolve()
		} catch (e) {
			//TODO handle the exception
			reject(e)
		}


	})
}

export function getbase64(base64data) {
	const fsm = wx.getFileSystemManager();
	const FILE_BASE_NAME = 'tmp_base64src';

	return new Promise((resolve, reject) => {
		const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
		if (!format) {
			reject(new Error('ERROR_BASE64SRC_PARSE'));
		}
		const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`;
		const buffer = wx.base64ToArrayBuffer(bodyData);
		fsm.writeFile({
			filePath,
			data: buffer,
			encoding: 'binary',
			success() {
				resolve(filePath);
			},
			fail() {
				reject(new Error('ERROR_BASE64SRC_WRITE'));
			},
		});
	});

}
/**
 * 
 * @param {*} showStroke 是否显示描边
 * @param {*} text 文本
 * @param {*} x 中心点x坐标
 * @param {*} y 中心点y坐标
 * @param {*} maxWidth 最大宽度
 * @param {*} lineHeight 行高
 * @returns 
 */
export function draw_wrapText(ctx, str, x, y, maxWidth, lineHeight, color) {
	return new Promise((resolve, reject) => {
		try {
			let lines = []
			let strTop = y
			let txt = ''
			for (let i in str) {
				ctx.font = `12px sans-serif`
				if (ctx.measureText(txt + str[i]).width > maxWidth === true) {
					// 画文字
					lines.push(txt)
					txt = str[i]
				} else {
					txt += str[i]
					if (i == str.length - 1) {
						lines.push(txt)
					}
				}
			}
			console.log(lines, 'lines', color);
			for (let i = 0; i < lines.length; i++) {
				ctx.setFillStyle(color || '#333')
				ctx.font = ` 12px  sans-serif`
				ctx.fillText(lines[i], x, strTop, maxWidth)
				strTop += lineHeight
			}
			resolve()
		} catch (e) {
			reject(e)
		}
	})

};